﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using myBoard.App.Business;
using myBoard.App.Model;

namespace myBoard.App
{
    public partial class ConfigurationForm : Form
    {
        Configuration _currentConf;

        public ConfigurationForm()
        {
            InitializeComponent();
            Application.EnableVisualStyles();
            _mouseActivationBut.Items.AddRange(myBoardKeys.MouseButtons.ToArray());
            _keyboardActivationKey.Items.AddRange(myBoardKeys.KeyboardButtons.ToArray());

            ConfigurationSerializer confSerializer = new ConfigurationSerializer();
            _currentConf = confSerializer.DeSerialize();

            _activationCornerList.SelectedItem = _currentConf.ActivationCorner.ToString();

            if (_currentConf.MyBoardActivationType == Enum.GetName(typeof(myBoardActivationType), myBoardActivationType.Mouse))
            {
                _mouseActivation.Checked = true;
                _keyboardActivation.Checked = false;
            }
            else
            {
                _keyboardActivation.Checked = true;
                _mouseActivation.Checked = false;
            }

            if(_mouseActivationBut.Items.Contains(_currentConf.MouseActivationButton))
                _mouseActivationBut.Text = _currentConf.MouseActivationButton;

            if (_keyboardActivationKey.Items.Contains(_currentConf.KeyboardActivationKey))
                _keyboardActivationKey.Text = _currentConf.KeyboardActivationKey;
            
        }

        private void _mouseActivation_CheckedChanged(object sender, EventArgs e)
        {
            UpdateActivationPanel();
        }

        private void _keyboardActivation_CheckedChanged(object sender, EventArgs e)
        {
            UpdateActivationPanel();
        }

        private void UpdateActivationPanel()
        {
            if (_mouseActivation.Checked)
            {
                _mouseActivationPanel.Enabled = true;
                _keyboardActivationPanel.Enabled = false;
            }
            else
            {
                _mouseActivationPanel.Enabled = false;
                _keyboardActivationPanel.Enabled = true;
            }
        }

        private void _saveBt_Click(object sender, EventArgs e)
        {
            InstancesSingleton.Instances.CurrentConfiguration.MyBoardActivationType = _mouseActivation.Checked ? Enum.GetName(typeof(myBoardActivationType), myBoardActivationType.Mouse) : Enum.GetName(typeof(myBoardActivationType), myBoardActivationType.Keyboard);
            InstancesSingleton.Instances.CurrentConfiguration.MouseActivationButton = _mouseActivationBut.Text;
            InstancesSingleton.Instances.CurrentConfiguration.KeyboardActivationKey = _keyboardActivationKey.Text;
            InstancesSingleton.Instances.CurrentConfiguration.ActivationCorner = (DesktopCorners)Enum.Parse(typeof(DesktopCorners), _activationCornerList.SelectedItem.ToString());
            ConfigurationSerializer confSerializer = new ConfigurationSerializer();
            confSerializer.Serialize(InstancesSingleton.Instances.CurrentConfiguration);
            this.Close();
        }
    }
}
